package cn.com.headfree.aoutdata.dao;


import cn.com.headfree.aoutdata.model.XuanChengDeptAndUser;
import cn.com.headfree.entity.*;
import cn.com.headfree.vo.SysDictionaryInfoVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.*;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2021/9/27
 **/
@Repository("outDataDao")
public class OutDataDao extends BaseDaoImpl<XuanChengDeptAndUser> {


    public List<TOrgDept> findmaxSort() {
        String sql = "select max(sort) sort from t_org_dept t ";
        return this.findAllBySQL(TOrgDept.class, sql, null, false,new Object[]{});
    }


    public List<TOrgDept> findAll() {
        String sql = "SELECT org_name name,org_dept_id id from xuan_cheng_dept_and_user  where zjybj not like  '%减员%'  GROUP BY org_dept_id ";
        return this.findAllBySQL(TOrgDept.class, sql, null, false,new Object[]{});
    }


    public List<TOrgDept> selectOrgfromXUN(String deptId){
        String sql = "SELECT  org_dept_id p_id,demp_no id ,dept_name name from xuan_cheng_dept_and_user where  zjybj not like  '%减员%'  and org_dept_id = ? GROUP BY demp_no";
        StringBuilder sb = new StringBuilder(sql);
        return this.findAllBySQL(TOrgDept.class, sb.toString(), null, false, new Object[] {deptId});
    }


    public void insertOrgDept(TOrgDept orgDept){
        String sql = "INSERT INTO t_org_dept (id, path, is_head_dept, limit_start, level, name, limit_end, p_id, sort, is_audit_unit) VALUES ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{path,jdbcType=VARCHAR},").append("#{is_head_dept,jdbcType=VARCHAR},")
                .append("#{limit_start,jdbcType=VARCHAR},").append("#{level,jdbcType=VARCHAR},")
                .append("#{name,jdbcType=VARCHAR},").append("#{limit_end,jdbcType=VARCHAR},")
                .append("#{p_id,jdbcType=VARCHAR},").append("#{sort,jdbcType=VARCHAR},")
                .append("#{is_audit_unit,jdbcType=VARCHAR})");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", orgDept.getId());
        params.put("path", orgDept.getPath());
        params.put("is_head_dept", orgDept.getIsHeadDept());
        params.put("limit_start", orgDept.getLimitStart());
        params.put("level", orgDept.getLevel());
        params.put("name", orgDept.getName());
        params.put("limit_end", orgDept.getLimitEnd());
        params.put("p_id", orgDept.getpId());
        params.put("sort", orgDept.getSort());
        params.put("is_audit_unit", orgDept.getIsAuditUnit());
        this.executeSql(sqlBuilder.toString(), params);
    }


    public void insertDeptEmpRela(TDeptEmpRela deptEmpRela ){
        String sql = "INSERT INTO t_dept_emp_rela (id, erp_no, org_id, dept_id) VALUES ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{erp_no,jdbcType=VARCHAR},").append("#{org_id,jdbcType=VARCHAR},")
                .append("#{dept_id,jdbcType=VARCHAR})");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", deptEmpRela.getId());
        params.put("erp_no", deptEmpRela.getErpNo());
        params.put("org_id", deptEmpRela.getOrgId());
        params.put("dept_id", deptEmpRela.getDeptId());
        this.executeSql(sqlBuilder.toString(), params);
    }


    public List<XuanChengDeptAndUser> selectxuancheng(){
        String sql = " SELECT * from xuan_cheng_dept_and_user where zjybj not like  '%减员%' ";
        return this.findAllBySQL(XuanChengDeptAndUser.class, sql, null, false,new Object[]{});
    }


    public List<TEmpBaseInfo> findEmpBaseByErpNo(String erpNo) {
        Object[] params=new Object[1];
        StringBuilder sql=new StringBuilder("select * from t_emp_base_info g where g.erp_no =?");
        // g.erp_no='11238555'
        params[0]=erpNo;
        return this.findAllBySQL(TEmpBaseInfo.class,sql.toString(),null,false,params);
    }

    public List<TDeptEmpRela> findDeptEmpRelaByErpNo(String erpNo) {
        Object[] params=new Object[1];
        StringBuilder sql=new StringBuilder("select * from t_dept_emp_rela g where g.erp_no =?");
        // g.erp_no='11238555'
        params[0]=erpNo;
        return this.findAllBySQL(TDeptEmpRela.class,sql.toString(),null,false,params);
    }


    public void addBaseInfo(TEmpBaseInfo baseInfo) {
        String sql = "insert into t_emp_base_info (id,erp_no,name,sex,birth_date,nation,birth_place,born_place,politi_status,id_card_no,mobile_phone,level,salary_grade,party_date,job_date,unit,dept,employee_level,post_level,cur_post,real_post,org_id,creater_id,creater_name,create_time,updater_id,updater_name,update_time,del_sign,is_valid,audit_state,orignal_id,flow_status,business_type_id,isc_user_id,primitive_id) values ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{erp_no,jdbcType=VARCHAR},")
                .append("#{name,jdbcType=VARCHAR},").append("#{sex,jdbcType=VARCHAR},")
                .append("#{birth_date,jdbcType=DATE},").append("#{nation,jdbcType=VARCHAR},")
                .append("#{birth_place,jdbcType=VARCHAR},").append("#{born_place,jdbcType=VARCHAR},").append("#{politi_status,jdbcType=VARCHAR},")
                .append("#{id_card_no,jdbcType=VARCHAR},").append("#{mobile_phone,jdbcType=VARCHAR},")
                .append("#{level,jdbcType=VARCHAR},").append("#{salary_grade,jdbcType=VARCHAR},")
                .append("#{party_date,jdbcType=DATE},").append("#{job_date,jdbcType=DATE},")
                .append("#{unit,jdbcType=VARCHAR},").append("#{dept,jdbcType=VARCHAR},")
                .append("#{employee_level,jdbcType=VARCHAR},").append("#{post_level,jdbcType=VARCHAR},")
                .append("#{cur_post,jdbcType=VARCHAR},").append("#{real_post,jdbcType=VARCHAR},")
                .append("#{org_id,jdbcType=VARCHAR},").append("#{creater_id,jdbcType=VARCHAR},")
                .append("#{creater_name,jdbcType=VARCHAR},").append("#{create_time,jdbcType=DATE},")
                .append("#{updater_id,jdbcType=VARCHAR},").append("#{updater_name,jdbcType=VARCHAR},")
                .append("#{update_time,jdbcType=DATE},").append("#{del_sign,jdbcType=INTEGER},")
                .append("#{is_valid,jdbcType=INTEGER},").append("#{audit_state,jdbcType=INTEGER},").append("#{orignal_id,jdbcType=VARCHAR},")
                .append("#{flow_status,jdbcType=INTEGER},").append("#{business_type_id,jdbcType=VARCHAR},")
                .append("#{isc_user_id,jdbcType=VARCHAR},").append("#{primitive_id,jdbcType=VARCHAR})");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", baseInfo.getId());
        params.put("erp_no", baseInfo.getErpNo());
        params.put("name", baseInfo.getName());
        params.put("sex", baseInfo.getSex());
        params.put("birth_date", baseInfo.getBirthDate());
        params.put("nation", baseInfo.getNation());
        params.put("birth_place", baseInfo.getBirthPlace());
        params.put("born_place", baseInfo.getBornPlace());
        params.put("politi_status", baseInfo.getPolitiStatus());
        params.put("id_card_no", baseInfo.getIdCardNo());
        params.put("mobile_phone", baseInfo.getMobilePhone());
        params.put("level", baseInfo.getLevel());
        params.put("salary_grade", baseInfo.getSalaryGrade());
        params.put("party_date", baseInfo.getPartyDate());
        params.put("job_date", baseInfo.getJobDate());
        params.put("unit", baseInfo.getUnit());
        params.put("dept", baseInfo.getDept());
        params.put("employee_level", baseInfo.getEmployeeLevel());
        params.put("post_level", baseInfo.getPostLevel());
        params.put("cur_post", baseInfo.getCurPost());
        params.put("real_post", baseInfo.getRealPost());
        params.put("org_id", baseInfo.getOrgId());
        params.put("creater_id", baseInfo.getCreaterId());
        params.put("creater_name", baseInfo.getCreaterName());
        params.put("create_time", new Timestamp(new Date().getTime()));
        params.put("updater_id", baseInfo.getUpdaterId());
        params.put("updater_name", baseInfo.getUpdaterName());
        params.put("update_time", new Timestamp(new Date().getTime()));
        params.put("del_sign", baseInfo.getDelSign());
        params.put("is_valid", baseInfo.getIsValid());
        params.put("audit_state", baseInfo.getAuditState());
        params.put("orignal_id", baseInfo.getOrignalId());
        params.put("flow_status",baseInfo.getFlowStatus());
        params.put("business_type_id",baseInfo.getBusinessTypeId());
        params.put("isc_user_id",baseInfo.getIscUserId());
        params.put("primitive_id",baseInfo.getPrimitiveId());
        this.executeSql(sqlBuilder.toString(), params);
    }


    public SysDictionaryInfoVo getSysDictionaryInfo(String pname,String name) {
        if(StringUtils.isBlank(pname)){
            pname = null;
        }
        if(StringUtils.isBlank(name)){
            name=null;
        }
        String sql = "SELECT t.* from t_sys_dictionary_info t where t.p_id = (SELECT id from t_sys_dictionary_info si where si.p_id ='6d81ed6c864046ffb564e78912ab2c10' and si.name = ?)   " +
                " and t.name LIKE concat('%',?,'%')  ORDER BY t.value LIMIT 1";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        Object[] params=new Object[2];
        params[0] = pname;
        params[1] = name;
        return this.findUnique(SysDictionaryInfoVo.class, sqlBuilder.toString(),params);
    }

}
